package com.zgw.his.design.model.property.edit.dialog;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import com.zgw.his.common.ValueUtil;
import com.zgw.his.design.common.LayoutUtil;
import com.zgw.his.design.common.SWTResourceManager;
import com.zgw.his.design.resource.BaseResource;
import com.zgw.his.server.emr.entity.Telement;

/**
 * 病历元素查找
 * */
public class ElementDialog extends Dialog {

	protected String result;
	protected Shell shell;
	private Text text;
	private Tree tree;
	private String code;

	private final Color color_gray = SWTResourceManager
			.getColor(SWT.COLOR_GRAY);

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public ElementDialog(Shell parent, int style, String code) {
		super(parent, style);
		setText("病历元素查找");
		this.code = code;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public String open() {
		createContents();
		Display display = getParent().getDisplay();
		LayoutUtil.centerShell(display, shell);
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.MAX
				| SWT.APPLICATION_MODAL);
		shell.setSize(495, 572);
		shell.setText("标识项查找");
		shell.setLayout(null);
		{
			text = new Text(shell, SWT.BORDER);
			text.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));
			text.setMessage("请在此输入名称、编号进行查找");
			text.setBounds(10, 10, 469, 25);
		}
		{
			tree = new Tree(shell, SWT.BORDER);
			tree.setBounds(10, 45, 469, 446);
			tree.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));
		}

		{
			Button btno = new Button(shell, SWT.NONE);
			btno.setBounds(329, 504, 72, 25);
			btno.setText("确定&O");
			btno.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					if (tree.getSelection().length > 0)
						doOk();
					else {
						MessageDialog.openError(shell, "错误 ", "请选择一项记录");
					}
				}
			});
		}
		{
			Button btnc = new Button(shell, SWT.NONE);
			btnc.setBounds(407, 504, 72, 25);
			btnc.setText("关闭&C");
			btnc.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					shell.close();
				}
			});
		}
		initializeListenenr();
		text.setText(code == null ? "" : code);
		text.setSelection(0, text.getText().length());
		doFilter();
	}

	private void initializeListenenr() {
		tree.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				doOk();
			}
		});
		text.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				doFilter();
			}
		});
	}

	protected void doOk() {
		if (tree.getSelection().length > 0) {
			TreeItem item = tree.getSelection()[0];
			if (item.getData(item.getText()) != null) {
				result = (String) item.getData(item.getText());
				shell.close();
			}
		}
	}

	private void doFilter() {
		String val = text.getText();
		List<Telement> list = new ArrayList<Telement>();
		if (ValueUtil.hasValue(val)) {
			for (Telement r : BaseResource.getElements()) {
				if (r.getFid() == null || r.getFname() == null)
					continue;
				if (r.getFcode().indexOf(val) > -1
						|| r.getFname().indexOf(val) > -1) {
					list.add(r);
				}
			}
		} else {
			list.addAll(BaseResource.getElements());
		}
		buildTree(list);
	}

	private void buildTree(List<Telement> list) {
		tree.removeAll();
		TreeItem item;
		for (Telement t : list) {
			if (list.indexOf(t) > 300)
				break;
			item = new TreeItem(tree, SWT.NONE);
			item.setText(t.getFname() + " -" + t.getFcode());
			item.setData(item.getText(), t.getFcode());
		}

	}
}
